package xkp.lesson.springboot_shiro.exception;

import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.authz.AuthorizationException;
import org.springframework.dao.DuplicateKeyException;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;
import org.springframework.web.servlet.NoHandlerFoundException;
import xkp.lesson.springboot_shiro.entity.JsonData;

/**
 * 统一异常处理器
 *
 */
@RestControllerAdvice
@Slf4j
public class GlobalExceptionHandler {
	/*@Autowired
	private SysExceptionLogService sysExceptionLogService;*/
	/**
	 * 处理自定义异常
	 */
	@ExceptionHandler(value = CustomException.class)
	public JsonData handleCustomException(CustomException e){
		JsonData jsonData = new JsonData();
		jsonData.setCode(e.getCode());
		jsonData.setMsg(e.getMsg());


		return jsonData;
	}

	@ExceptionHandler(value = NoHandlerFoundException.class)
	public JsonData handlerNoFoundException(Exception e) {
		log.error(e.getMessage(), e);
		return new JsonData(404,"路径不存在，请检查路径是否正确");
	}

	@ExceptionHandler(value = DuplicateKeyException.class)
	public JsonData handleDuplicateKeyException(DuplicateKeyException e){
		log.error(e.getMessage(), e);
		return new JsonData("数据库中已存在该记录");
	}

	@ExceptionHandler(value = AuthorizationException.class)
	public JsonData handleAuthorizationException(AuthorizationException e){
		log.error(e.getMessage(), e);
		return new JsonData("没有权限，请联系管理员授权");
	}

	@ExceptionHandler(value = Exception.class)
	public JsonData handleException(Exception e){
		//saveExceptionLog(e);
		return new JsonData(0,e.getMessage());
	}

	/*private void saveExceptionLog(Throwable e){
		ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
		HttpServletRequest request = attributes.getRequest();
		String uri = request.getRequestURI();
		String httpMethod = request.getMethod();
		String ip = request.getRemoteAddr();
		String useragent = request.getHeader("user-agent");
		log.info("URI: " + uri);
		//请求的方法名
		log.info("HTTP_METHOD: " + httpMethod);
		log.info("IP: "+ ip);
		Enumeration<String> enu = request.getParameterNames();
		JSONObject params = new JSONObject();
		while (enu.hasMoreElements()) {
			String name = enu.nextElement();
			params.put(name, request.getParameter(name));
			log.info("name:{},value:{}", name, request.getParameter(name));
		}
		log.info("params:" +params);
		log.error("exception: ",e);
		*//*SysExceptionLogEntity entity = new SysExceptionLogEntity();

		entity.setUri(uri);
		entity.setMethod(httpMethod);
		entity.setParams(params.toJSONString());
		entity.setIp(ip);
		entity.setUseragent(useragent);
		//当前时间
		Date now = new Date();
		entity.setCreateDate(now);*//*

		StackTraceElement[] stackTrace = e.getStackTrace();

		StringBuilder straceTraceString = new StringBuilder();
		for (int i = 0 ;i < stackTrace.length; i++){
			straceTraceString.append(stackTrace[i]);
			straceTraceString.append("<br/>");

		}

		log.info("exception length:"+straceTraceString.toString().length());
		*//*entity.setException(straceTraceString.toString());
		sysExceptionLogService.save(entity);*//*
	}*/
}
